﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;

public partial class System_Pages_Security_Roles : SiteFox.Base.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            this.LoadRoles();
        }
    }

    #region ControlsEvents
    protected void imb_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_AddOrEdit.Visible = true;
    }
    protected void lib_AddOrEdit_Close_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = false;
        pan_AddOrEdit.Visible = false;
        this.ResetAddOrEdit();
    }
    protected void lib_AddOrEdit_Save_Click(object sender, EventArgs e)
    {
        if (hf_AddOrEdit_Id.Value == "")
        {
            SiteFox.Business.Model.Group _Group = new SiteFox.Business.Model.Group();
            _Group.Name = ucc_AddOrEdit_Name.Value;
            _Group.Description = ucc_AddOrEdit_Description.Value;
            SiteFox.Business.Manager.Group.Create(_Group);
        }
        else 
        {
            SiteFox.Business.Model.Group _Group = SiteFox.Business.Manager.Group.GetById(Convert.ToInt32(hf_AddOrEdit_Id.Value));
            _Group.Name = ucc_AddOrEdit_Name.Value;
            _Group.Description = ucc_AddOrEdit_Description.Value;
            SiteFox.Business.Manager.Group.Edit(_Group);
        }
        pan_Background.Visible = false;
        pan_AddOrEdit.Visible = false;
        this.ResetAddOrEdit();
        this.LoadRoles();
    }
    protected void lib_Role_Edit_Click(object sender, EventArgs e) 
    {
        pan_Background.Visible = true;
        pan_AddOrEdit.Visible = true;
        hf_AddOrEdit_Id.Value = ((LinkButton)(sender)).CommandArgument;
        SiteFox.Business.Model.Group _Group = SiteFox.Business.Manager.Group.GetById(Convert.ToInt32(hf_AddOrEdit_Id.Value));
        ucc_AddOrEdit_Name.Value = _Group.Name;
        ucc_AddOrEdit_Description.Value = _Group.Description;
    }
    protected void lib_Role_Delete_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = true;
        pan_Delete.Visible = true;
        hf_Delete_Id.Value = ((LinkButton)(sender)).CommandArgument;
        lib_Delete_Ok.Visible = ucc_Delete_Text.Visible = true;
        ucc_Delete_Message.Visible = false;
    }
    protected void lib_Delete_Close_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = false;
        pan_Delete.Visible = false;
        hf_Delete_Id.Value = "";
    }
    protected void lib_Delete_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Manager.Group.Delete(Convert.ToInt32(hf_Delete_Id.Value));
        pan_Background.Visible = false;
        pan_Delete.Visible = false;
        hf_Delete_Id.Value = "";
        this.LoadRoles();
        //int _NumberOfUsersInRole = Roles.GetUsersInRole(hf_RoleName.Value).Length;
        //if (_NumberOfUsersInRole == 0)
        //{
        //    Roles.DeleteRole(hf_RoleName.Value);
        //    pan_Delete.Visible = false;
        //}
        //else
        //{
        //    ucc_Delete_Message.Value = Resources.dcCommon.dc_RoleHasUsers;
        //    ucc_Delete_Message.Visible = true;
        //    ucc_Delete_Text.Visible = lib_Delete_Ok.Visible = false;
        //}
    }
    #endregion

    #region Methods
    void LoadRoles() 
    {
        gv_Roles.DataSource = SiteFox.Business.Manager.Group.GetAll();
        gv_Roles.DataBind();
    }
    void ResetAddOrEdit() 
    {
        ucc_AddOrEdit_Description.Value = "";
        ucc_AddOrEdit_Message.Value = "";
        ucc_AddOrEdit_Name.Value = "";
        hf_AddOrEdit_Id.Value = "";
    }
    #endregion
}